package com.pos43.business.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pos43.business.dao.IOrderItemDao;
import com.pos43.business.dao.IOrdersDao;
import com.pos43.business.dao.IProductDao;
import com.pos43.domain.OrderItem;
import com.pos43.domain.Orders;
import com.pos43.domain.Product;

public class OrderItemDao extends HibernateDaoSupport implements IOrderItemDao{
	
	IOrdersDao orderDao = new OrdersDao();
	IProductDao productDao = new ProductDao();
	
	public OrderItem get(Orders order, Product product){
		OrderItem orderItem = new OrderItem(order, product);
		return getHibernateTemplate().get(OrderItem.class, orderItem);
	}
	
	public OrderItem get(Integer order_id, Integer product_id){
		OrderItem orderItem = new OrderItem(orderDao.get(order_id), productDao.get(product_id));
		return getHibernateTemplate().get(OrderItem.class, orderItem);
	}

	public Integer save(OrderItem orderItem){
		getHibernateTemplate()
			.save(orderItem);
		return 1;
	}
	
	public void update(OrderItem orderItem){
		getHibernateTemplate().update(orderItem);
	}
	
	public void delete(OrderItem orderItem){
		getHibernateTemplate().delete(orderItem);
	}
	
	public void delete(Integer order_id, Integer product_id){
		getHibernateTemplate().delete(get(order_id, product_id));
	}
	
	public List<OrderItem> findAll(){
		return (List<OrderItem>)getHibernateTemplate()
				.find("from OrderItem");
	}

	public List<OrderItem> findById(Integer id) {
		
		String hql = "from OrderItem i where i.order.orderId =" + id + " or i.product.pid '%" + id + "%'";
		return this.getSession()
				.createQuery(hql)
				.list();
	}

	public IOrdersDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(IOrdersDao orderDao) {
		this.orderDao = orderDao;
	}

	public IProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(IProductDao productDao) {
		this.productDao = productDao;
	}

	

}
